import axios from '../../http/http.js'
import * as url from '../../api/ygr_goods_api.js'
const state = {
    tableList: [], //要渲染的表格数据
    maxPage: 1, //最大页数
    now: 1, //当前页
    max: 12, //总数据长度
    allList:[]

};
const actions = {
    getData({ commit }) {//获取列表数据
        return axios.get(url.GETGOODSDATA)
            .then(res => {
                commit('addtableData', res)
            })
            .catch(err => {
                console.log(err)
            })
    },
    changePage({ commit }, currentPage) {//触发当前页改变事件
        commit('changePage',currentPage)
        // this.dispatch('getData')
    }

};
const mutations = {
    addtableData(state, obj) {//列表数据进行存值
        let data = obj.data.data
        state.allList=data;
        // state.tableList = data.filter((item, index) => {
        //     return index < state.now * 4 && index >= (state.now - 1) * 4
        // });
        state.maxPage = Math.ceil(data.length / 4);
        state.max = data.length

    },
    changePage(state, index) {//改变当前页
        state.now=index;
    }

}
const getters = {
    mylist(state) {//监听数据的的当前页和总数据自动改变表格渲染数据
        return {
            tableList: state.allList.filter((item, index) => {
                return index < state.now * 4 && index >= (state.now - 1) * 4
            }),
            maxPage: Math.ceil(state.allList.length / 4),
            now: state.now,
            max: state.allList.length
        }
    }

};
export default {
    state,
    actions,
    mutations,
    getters
}